package cc.wanforme.chipmunity.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.Base64Utils;

import cc.wanforme.chipmunity.common.consts.EncodeEnum;

/**富文本工具类
 * @author wanne
 * 2019年8月15日
 * 
 */
public class RichTextUtil {
	private static final String FAIL_MSG="iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAHPElEQVRYR8VXf2xVZxl+nnMKF+rW9lwKbWE/hN5zE2Eoy2TxF7Jkc8MocyQbGn9OQjd6Tp26zcVNJRqDUWFTGeeUjkziYjRgRLYx3Q/jMohOg85BMNnuuS0QWAsDz7mUbi3tvecx57a3tB2DEv/g++fmfPc9z/s87/t+7/ce4hIvXmL/ILbLtE50fVjALAOxBdASZQGwAFkU0wCeDV37ZxPJ1rUHNxgxHgTQKyqEGIIKKYaxwRBSyJIRDlQVw/4h4yTusc9MxKDl5dtIPXr+SKgndLKzx9rUeblFBP9OIjXpKAr9CUElRIGuM3HKYdoP7gXwsIAjAPZAPCriaAJK6BcEGEOrCk52a8XRZRuDmVOrtA9gk8SfinjZgOol1AuoN6jyL5NnYibFehA1E4kK+DbrvNydBpmAbw0de1XFKO0Fz4G4WcLLkWt/ZPTljUEqbWoPyMUCdkaOvWJSEejQlGocrk8NluphxOtILBd4N+s2BZ8xDOyU8GTk2rclYLVecLtJ/E5Ssciqa047814fJeYHPwCwVtLB6Mz0Bbj3yv5JERhjlPaD7QDuKAl30PK7lhCl3ZJ2R252aWKX9oMcABvSxtDNfn187vNfNqhfSYgF3lRwMy9eNAEv92eQN5bIG5neFCyAgQMCDkSOvTABs/zcLoKfgtAfw/hiwW3eMdaJ5eV/QuoBCL1DMhafbmtOCI9blp//JKTvkWgG8ExYn2nBSpbK+F7u3yQXxdK1rO441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Gs/b3mBB2o1wGOEjknsAdkjqMcQe2TEPTHNY4Mq9vTPyB4bvQn94BUC1xZlXN/rNidtHKh99NBc0xzqOpcaAU8NkS1TpHKLLk2dNqt39ZVh2sttBXnnZCKQdEkSJxNyFLPJUS6Kdq+byQ8XYUdnLUtxIVFIaWdMHgR4iGAXi/Ge5KodDZ14V+hmtpQdd3RX15b6Gg0aTXGsJgNsBNVEoElCE6BGJnMjMSuZLceSLU6dNiMRMrpp+UGcGIWtGQOkJipLe/kWUI9BeD507Vsmo3zUZrvM6uNHGqZMGZpdSWXUmjETP2cJeLmTJGcMkg19rZk3JzoYuXpPlPdl3MBSfChszPSMnQEuRCrBMDEYCogix06+N86GpTIHCmgHVCRQI7AWQApCe+TaT6f94FkAo+rLuQVOCHgDLNdIN8luxOwW2S2V3igaRnffmuYTidrajqDZLCGfdNPIsTPjCFh+8FcCZ8fvcXL0+9DJ3l7jdS42Ga+HMIdQYzLVXEj1SN8YSk4LhEJy7UPaG7rZ68cRKI/iQIsSRcRxSsdF47iMuLtoVu1/q2Xe8Xc4S4pwcLDB5FBjbLCBiBtJNkhspOIGDRdlA4ArkoGl8r6A30SO/YVxBCaj5P+yefy1y2sGqhpMAw1Rf+qVyvfEJf86/h/1SXU2J43TJQAAAABJRU5ErkJggg==";
	
	// 参考：https://blog.csdn.net/qq_24484085/article/details/89602723
	
	/** 将html中img标签的src的url对应的图片替换成base64的图片，
	 * 注意是网络图片，并且能使用GET请求获取
	 * @return
	 */
	public static String encodeImgBase64(String source) {
		StringBuffer sb = new StringBuffer();
		// 目前img标签标示有3种表达式
		// <img alt="" src="1.jpg"/> <img alt="" src="1.jpg"></img> <img alt=""
		// src="1.jpg">
		// 开始匹配content中的<img />标签
		Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
		Matcher m_img = p_img.matcher(source);
		boolean result_img = m_img.find();
		if (result_img) {
			while (result_img) {
				StringBuffer sbSrc = new StringBuffer();
				// 获取到匹配的<img />标签中的内容
				String str_img = m_img.group(2);
				// 开始匹配<img />标签中的src
				Pattern p_src = Pattern
						.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
				Matcher m_src = p_src.matcher(str_img);
				if (m_src.find()) {
					// 取出img的src的url路径
					String str_src = m_src.group(3);
					// 将图片转成base64码
					String base = imgToBase64(str_src);
					m_src.appendReplacement(sbSrc, " src=\"" + str_src + "\" ");
					m_img.appendReplacement(sb,
							"<img class=\"img-responsive\" src=\"data:image/png;base64,"
									+ base + "\" />");
				}
				// 结束匹配<img />标签中的src
				// 匹配source中是否存在下一个<img />标签，有则继续以上步骤匹配<img />标签中的src
				result_img = m_img.find();
			}
			m_img.appendTail(sb);
			return sb.toString();
		} else {
			return source;
		}
	}
	
	/**根据图片的路径转成base64码
	 * @param imgURL
	 * @return
	 */
	protected static String imgToBase64(String imgURL) {
		boolean flag = false;
		ByteArrayOutputStream outPut = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		try {
			// 创建URL
			URL url = new URL(imgURL);
			// 创建链接
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(10 * 1000);

			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				flag = false;// 连接失败/链接失效/图片不存在
			}
			InputStream inStream = conn.getInputStream();
			int len = -1;
			while ((len = inStream.read(data)) != -1) {
				outPut.write(data, 0, len);
			}
			inStream.close();
			flag = true;
		} catch (IOException e) {
			flag = false;
		}
		// 异常情况下 返回一个图片丢失的base64的图片
		// String failImg = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAHPElEQVRYR8VXf2xVZxl+nnMKF+rW9lwKbWE/hN5zE2Eoy2TxF7Jkc8MocyQbGn9OQjd6Tp26zcVNJRqDUWFTGeeUjkziYjRgRLYx3Q/jMohOg85BMNnuuS0QWAsDz7mUbi3tvecx57a3tB2DEv/g++fmfPc9z/s87/t+7/ce4hIvXmL/ILbLtE50fVjALAOxBdASZQGwAFkU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68cRKI/iQIsSRcRxSsdF47iMuLtoVu1/q2Xe8Xc4S4pwcLDB5FBjbLCBiBtJNkhspOIGDRdlA4ArkoGl8r6A30SO/YVxBCaj5P+yefy1y2sGqhpMAw1Rf+qVyvfEJf86/h/1SXU2J43TJQAAAABJRU5ErkJggg==";
		// 对字节数组Base64编码
		if (flag == true) {
			return Base64Utils.encodeToString(outPut.toByteArray());
			/*
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(outPut.toByteArray());
			*/
		} else {
			return FAIL_MSG;
		}
	}
	
	public static void main(String[] args) {
		String imgUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565860677187&di=f33c824ab5ab8f468115f27d047431fc&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fa77c67ea01f32fe94053b653caefb1dc5783bf6947024-zYrxl3_fw658";
		String testHtml = "<head><meta charset=\"utf-8\"/></head>"
				+ "<body><p>测试文本</p>"
				+ "<img style=\"width:420px;height:360px;\" src=\"" + imgUrl + "\" />"
				+ "<p>新文字</p></body>";
		
		String result = encodeImgBase64(testHtml);
		
		//String saveFile = "D:/testRichText.rtf";
		// html方便直接查看，（浏览器中打开，要指定编码方式，不然可能会乱码）
		String saveFile = "D:/testRichText.html";
		
		try{
			FileOutputStream fos = new FileOutputStream(new File(saveFile));
			fos.write(result.getBytes(EncodeEnum.STRING_TO_BYTES.getName()));
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println(" Rich Text finished! ");
	}
}
